package com.cpo.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cpo.dao.LoginDao;
import com.cpo.dao.UserRegistrationDao;
import com.cpo.domain.Login;
import com.cpo.interfaces.ILoginDao;
import com.cpo.interfaces.LoginService;

@Service
public class LoginServiceImpl implements LoginService{

	@Autowired
	private ILoginDao loginDao;
	
	public boolean validateUser(Login login) {
	
		boolean IReturnValue = false;
		
		List<Login> user =  loginDao.getByUserName(login.getUserName());
		for(Login log: user){
		if(null!=log){
			if((log.getPassword().equals(login.getPassword()))){
				IReturnValue = true;
			}
			
		}
		}
		return IReturnValue;
}

	public void updateAttempt(Login login) {
		
		

		List<Login> user = loginDao.getByUserName(login.getUserName());
		
		for(Login login1:user){
			int i = login1.getNoOfAttempts();
			if(validateUser(login) && !isLoginLocked(login)){
				login1.setNoOfAttempts(0);
			}
			else
			login1.setNoOfAttempts(++i);
		loginDao.updateIncorrectAttempts(login1);
		}
	}

	public int checkNoOfAttempts(Login login) {
		int i=1;
		List<Login> login2 = loginDao.getByUserName(login.getUserName());
		for(Login log:login2){
			if(log.getNoOfAttempts()>=5 && log.getStatus().equals("A")){
				i=0;
				
				}
			}
		
		return i;
	}

	public boolean isLoginLocked(Login login) {
		boolean IReturnValue = false;
		List<Login> login3 =loginDao.getByUserName(login.getUserName());
		for(Login log1:login3){
			if(null!=log1){
			if(log1.getStatus().equals("L")){
				IReturnValue = true;
			}
			}
		}
		return  IReturnValue;
	}
	public List<Login> getUserDetails(Login login){
		
		List<Login> login2 = loginDao.getByUserName(login.getUserName());
		return login2;
	}
	
	public boolean isUserPresent(Login login){
		boolean IReturValue = true;
		List<Login> login3 =loginDao.getByUserName(login.getUserName());
		for(Login login2: login3){
		if(null!=login2){
			IReturValue = false;
		}
		}
		return IReturValue;
	}
	public void lockUserAccount(Login login){
		loginDao.lockAccount(login.getUserName());
	}
	
	public boolean checkTime(Login login){
		boolean IReturnValue = false;
		List<Login> login4 =loginDao.getByUserName(login.getUserName());
		for(Login log4:login4){
		Date lastFailedDateTime = log4.getLastFailedDateTime();
		Date ctDate = new Date();
		int diff = (int) ((ctDate.getTime()/60000)-(lastFailedDateTime.getTime()/60000));
		if(diff>=5){
		IReturnValue = true;
		}
		}
		return IReturnValue;
	}
	public void updateLock(Login login){
		List<Login> login5 = loginDao.getByUserName(login.getUserName());
		for(Login log5:login5){
			log5.setStatus("A");
			log5.setNoOfAttempts(0);
			loginDao.unlockAccount(log5);
		}
		
		
	}
}
